package com.seamtop;

import clojure.lang.Obj;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.seamtop.cuber.client.CuberClient;
import com.seamtop.cuber.common.entriy.IndexOperator;
import com.seamtop.cuber.common.util.DateUtil;
import com.seamtop.cuber.core.ESManager;
import com.seamtop.cuber.core.HBaseUtil;
import io.searchbox.core.Delete;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;

import java.util.Date;
import java.util.HashMap;

/**
 * Created by zongjunfeng on 2015/8/21.
 * 车源点击次数功能测试数据初始化
 */
public class BaseInit {

    public static void main(String [] args) throws Exception{
        //deleteHbaseTable();
        //createHbaseTable();
//        initCuberCarData();
//        initCarHitData();
        deleteAllDataOfTable("T_CAR");
        //deleteESData("s2");
    }

    public static void deleteESData(String type){
        try{
            String query = "{\"query\":{\"bool\":{\"must\":[{\"match_all\":{}}],\"must_not\":[],\"should\":[]}},\"from\":0,\"size\":10,\"sort\":[],\"facets\":{}}";
            Search search = new Search.Builder(query)
                    // multiple index or types can be added.
                    .addIndex("2sc")
                    .addType(type)
                    .build();
            SearchResult result = ESManager.getClient().execute(search);
            String re = result.getJsonString();
            JSONObject object = JSON.parseObject(re);
            JSONArray o = object.getJSONObject("hits").getJSONArray("hits");
            for(int i=0;i<o.size();i++){
                JSONObject t = o.getJSONObject(i);
                ESManager.getClient().execute(new Delete.Builder(t.getString("_id"))
                        .index("2sc")
                        .type(type)
                        .build());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void deleteHbaseTable() throws Exception{
        //HBaseUtil.dropTable("CUBER_CAR");
        HBaseUtil.dropTable("CUBER_STAT_RESULT");

    }

    public static void createHbaseTable() throws Exception{
        String[] str = new String[]{"base_family"};
        String tableName = "CUBER_STAT_RESULT";
        //HBaseUtil.createTable(tableName,str);
//        String tableName2 = "CUBER_STAT";
//        HBaseUtil.createTable(tableName2,str);
    }

    public static void deleteAllDataOfTable(String tableName) throws Exception{
        //HBaseUtil.deleteAllRecord("CUBER_STAT","");
        //HBaseUtil.deleteAllRecord("CUBER_CAR","");
        HBaseUtil.deleteAllRecord(tableName,"");
        System.out.println("ok-----------");
    }

    public static void initCuberCarData(){
        for(int i=0;i<5;i++){
            HashMap<String,Object> paramsMap = new HashMap<String, Object>();
            paramsMap.put("car_id",39);
            paramsMap.put("province_id",120000);
            paramsMap.put("city_id",121000);
            paramsMap.put("dealer_id",17919);
            paramsMap.put("color","黑色");
            paramsMap.put("mileage",23.2);
            paramsMap.put("create_date",new Date().getTime());
            try{
                CuberClient.INSTANCE.addIndex("T_CAR", paramsMap);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }


}
